<?php

require_once("database.php");
require_once("util.php");

class Order
{
	private $id;
	private $userId;
	private $commodityId;
	private $time;
	private $status;
	
	function __construct($id = 0,
						 $userId = 0,
						 $commodityId = 0,
						 $time = "0000-00-00",
						 $status = "HANDLING")
	{
		$this->id = $id;
		$this->userId = $userId;
		$this->commodityId = $commodityId;
		$this->time = $time;
		$this->status = $status;
	}
	
	public function setId($id) {$this->id = $id;}
	public function setUserId($userId) {$this->userId = $userId;}
	public function setCommodityId($commodityId) {$this->commodityId = $commodityId;}
	public function setTime($time) {$this->time = $time;}
	public function setStatus($status) {$this->status = $status;}
	
	public function getId() {return $this->id;}
	public function getUserId() {return $this->userId;}
	public function getCommodityId() {return $this->commodityId;}
	public function getTime() {return $this->time;}
	public function getStatus() {return $this->status;}
}



class OrderDAO
{
	private $db;
	function __construct()
	{
		$this->db = new Database();
	}
	
	public function addOrder($order)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("insert into ESOrder(userId, commodityId, date) values(?, ?, ?)",
												array($order->getUserId(), $order->getCommodityId(), Util::getCurrentDate()));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function cancelOrder($orderId)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("update ESOrder set status = 'CANCELED' where id = ?", array($orderId));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function finishOrder($orderId)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("update ESOrder set status = 'SUCCEEDED' where id = ?", array($orderId));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function getCandidateOrderList()
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select ESOrder.id oId, ESUser.name uName, ESCommodity.id cId, ESCommodity.name cName, ESCommodity.description, ESCommodity.price, ESCommodity.discount, ESCommodity.picURL, ESOrder.date, ESOrder.status from ESCommodity, ESOrder, ESUser where ESCommodity.id = ESOrder.commodityId and ESUser.id = ESOrder.userId and ESOrder.status = 'HANDLING'", array());
			$rs = $prepStmt->executeQuery();
			while($rs->next())
			{
				$oId = $rs->value("oId");
				$uName = $rs->value("uName");
				$cId = $rs->value("cId");
				$cName = $rs->value("cName");
				$cDescription = $rs->value("description");
				$cPrice = $rs->value("price");
				$cDiscount = $rs->value("discount");
				$cPic = $rs->value("picURL");
				$oDate = $rs->value("date");
				$oStatus = $rs->value("status");
				$list[$oId] = array("oId" => $oId,
									"uName" => $uName,
									"cId" => $cId,
									"cName" => $cName,
									"cDescription" => $cDescription,
									"cPrice" => $cPrice,
									"cDiscount" => $cDiscount,
									"cPic" => $cPic,
									"oDate" => $oDate,
									"oStatus" => $oStatus);
			}
			
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
	
	public function getCandidateOrderListByUserId($userId)
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select ESOrder.id oId, ESCommodity.id cId, ESCommodity.name, ESCommodity.description, ESCommodity.price, ESCommodity.discount, ESCommodity.picURL, ESOrder.date, ESOrder.status from ESCommodity, ESOrder where ESCommodity.id = ESOrder.commodityId and ESOrder.userId = ? and ESOrder.status = 'HANDLING'", array($userId));
			$rs = $prepStmt->executeQuery();
			while($rs->next())
			{
				$oId = $rs->value("oId");
				$cId = $rs->value("cId");
				$cName = $rs->value("name");
				$cDescription = $rs->value("description");
				$cPrice = $rs->value("price");
				$cDiscount = $rs->value("discount");
				$cPic = $rs->value("picURL");
				$oDate = $rs->value("date");
				$oStatus = $rs->value("status");
				$list[$oId] = array("oId" => $oId,
									"cId" => $cId,
									"cName" => $cName,
									"cDescription" => $cDescription,
									"cPrice" => $cPrice,
									"cDiscount" => $cDiscount,
									"cPic" => $cPic,
									"oDate" => $oDate,
									"oStatus" => $oStatus);
			}
			
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
	
	public function getFinishedOrderListByUserId($userId)
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select ESOrder.id oId, ESCommodity.id cId, ESCommodity.name, ESCommodity.description, ESCommodity.price, ESCommodity.discount, ESCommodity.picURL, ESOrder.date, ESOrder.status from ESCommodity, ESOrder where ESCommodity.id = ESOrder.commodityId and ESOrder.userId = ? and ESOrder.status <> 'HANDLING'", array($userId));
			$rs = $prepStmt->executeQuery();
			while($rs->next())
			{
				$oId = $rs->value("oId");
				$cId = $rs->value("cId");
				$cName = $rs->value("name");
				$cDescription = $rs->value("description");
				$cPrice = $rs->value("price");
				$cDiscount = $rs->value("discount");
				$cPic = $rs->value("picURL");
				$oDate = $rs->value("date");
				$oStatus = $rs->value("status");
				$list[$oId] = array("oId" => $oId,
									"cId" => $cId,
									"cName" => $cName,
									"cDescription" => $cDescription,
									"cPrice" => $cPrice,
									"cDiscount" => $cDiscount,
									"cPic" => $cPic,
									"oDate" => $oDate,
									"oStatus" => $oStatus);
			}
			
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
}


?>